package com.qiyeji.real_estate.controller;

import com.qiyeji.real_estate.entity.payroll;
import com.qiyeji.real_estate.entity.payrollKey;
import com.qiyeji.real_estate.service.PayrollService;
import com.qiyeji.real_estate.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@RequestMapping("/Payroll")
@RestController
public class PayrollController {
    @Autowired
    PayrollService payrollService;

    @PostMapping("/AddPayroll")
    public Result addPayroll(@RequestBody Map<String, String> params) throws ParseException {
//        java.text.SimpleDateFormat formatter =  new SimpleDateFormat(  "yyyy-MM-dd" );
        Integer res = payrollService.addPayroll(new payroll(
                params.get("date"),
                params.get("sCode"),
                Float.parseFloat(params.get("baseSalary")),
                Float.parseFloat(params.get("bonus"))
        ));

        if (res == null)
            return Result.error();
        else
            return Result.success(res);
    }

    @PostMapping("/GetAllPayroll")
    public Result getAllPayroll() {
        List<payroll> res = payrollService.getAllPayroll();

        if (res == null)
            return Result.error();
        else
            return Result.success(res);
    }

    @PostMapping("/AlterPayroll")
    public Result alterPayroll(@RequestBody Map<String, String> params) {
//        java.text.SimpleDateFormat formatter =  new SimpleDateFormat(  "yyyy-MM-dd" );
        Integer res = payrollService.alterPayroll(new payroll(
                params.get("date"),
                params.get("sCode"),
                Float.parseFloat(params.get("baseSalary")),
                Float.parseFloat(params.get("bonus"))
        ));

        if (res == null)
            return Result.error();
        else
            return Result.success(res);
    }

    @PostMapping("/DeletePayroll")
    public Result deletePayroll(@RequestBody Map<String, String> params) {
//        java.text.SimpleDateFormat formatter =  new SimpleDateFormat(  "yyyy-MM-dd" );
        Integer res = payrollService.deletePayroll(new payrollKey(
                params.get("date"),
                params.get("sCode")
        ));
        if (res == null)
            return Result.error();
        else
            return Result.success(res);
    }

    @PostMapping("/GetPayrollBySName")
    public Result getPayrollBySName(@RequestBody Map<String, String> params) {
        List<payroll> res = payrollService.getPayrollBySName('%' + params.get("sName") + '%');

        if (res == null)
            return Result.error();
        else
            return Result.success(res);
    }

    @PostMapping("/GetPayrollByDate")
    public Result getPayrollByDate(@RequestBody Map<String, String> params) throws ParseException {
//        java.text.SimpleDateFormat formatter =  new SimpleDateFormat(  "yyyy-MM-dd" );
        List<payroll> res = payrollService.getPayrollByDate('%' + params.get("date") + '%');

        if (res == null)
            return Result.error();
        else
            return Result.success(res);
    }

    @PostMapping("/GetPayrollByD")
    public Result getPayrollByD(@RequestBody Map<String, String> params) {
        List<payroll> res = payrollService.getPayrollByD(params.get("dName"));

        if (res == null)
            return Result.error();
        else
            return Result.success(res);
    }
}
